package xgenerator.jdbc;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JdbcTemplate<T> {
	
	public Object queryForObject(String sql, Class requiredType) {
		Object obj = null;
		
		try {
			obj = requiredType.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return obj;
	}
	
	public Map queryForMap(String sql) {
		Map map = new HashMap();
		
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = JdbcContext.getJdbcContext().currentConnection().createStatement();
			rs = stmt.executeQuery(sql);
			ResultSetMetaData rsMetaData = rs.getMetaData();
			int columnCount = rsMetaData.getColumnCount();
			if(rs.next()) {
				for(int i=0; i<columnCount; i++) {
					map.put(rsMetaData.getColumnLabel(i), rs.getObject(i));
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcContext.close(stmt, rs);
		}
		
		return map;
	}
	
	public List queryForList(String sql) {
		List list = new ArrayList();
		
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = JdbcContext.getJdbcContext().currentConnection().createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()) {
				list.add(rs.getObject(0));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcContext.close(stmt, rs);
		}
		
		return list;
	}
	
	public int queryForInt(String sql) {
		int count = 0;
		
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = JdbcContext.getJdbcContext().currentConnection().createStatement();
			rs = stmt.executeQuery(sql);
			if(rs.next()) {
				count = rs.getInt(0);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcContext.close(stmt, rs);
		}
		return count;
	}
	
	public long queryForLong(String sql) {
		long count = 0;
		
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = JdbcContext.getJdbcContext().currentConnection().createStatement();
			rs = stmt.executeQuery(sql);
			if(rs.next()) {
				count = rs.getLong(0);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcContext.close(stmt, rs);
		}
		return count;
	}
	
	public int insert(String sql) {
		Statement stmt = null;
		try {
			stmt = JdbcContext.getJdbcContext().currentConnection().createStatement();
			return stmt.executeUpdate(sql);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcContext.close(stmt);
		}
	}
	
	public int update(String sql) {
		Statement stmt = null;
		try {
			stmt = JdbcContext.getJdbcContext().currentConnection().createStatement();
			return stmt.executeUpdate(sql);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcContext.close(stmt);
		}
	}
	
	public int delete(String sql) {
		Statement stmt = null;
		try {
			stmt = JdbcContext.getJdbcContext().currentConnection().createStatement();
			return stmt.executeUpdate(sql);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcContext.close(stmt);
		}
	}
	
	public int[] batchUpdate(String[] sql) {
		Statement stmt = null;
		try {
			stmt = JdbcContext.getJdbcContext().currentConnection().createStatement();
			for(int i=0; i<sql.length; i++) {
				stmt.addBatch(sql[i]);
			}
			return stmt.executeBatch();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcContext.close(stmt);
		}
	}
	
	public Map call(String sql) {
		Map map = new HashMap();
		
		Statement stmt = null;
		try {
			stmt = JdbcContext.getJdbcContext().currentConnection().createStatement();
			stmt.execute(sql);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcContext.close(stmt);
		}
		
		return map;
	}
	
	public void execute(String sql) {
		Statement stmt = null;
		try {
			stmt = JdbcContext.getJdbcContext().currentConnection().createStatement();
			stmt.execute(sql);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcContext.close(stmt);
		}
	}
	
}
